/**
 *自定义模板的租户处理
 */
; (function () {
  // 小于1024像素则不执行 (ipad pro)
  // if(window.innerWidth < 1024){
  // console.log('window.innerWidth :', window.innerWidth);
  if (window.innerWidth <= 1024) {
    return;
  }

  // 对模板数据进行处理
  window.initSoftDate = async (config, getNewApp, getSystemInfo) => {
    console.warn("正在初始化自定义模板展示");
    const systemInfo = await getSystemInfo();
    console.log('initSoftDate：', { config, systemInfo });


    // 查询需要修改的元素
    const qrcodeImg = document.querySelector("#tpl-qrcode-img");
    const title = document.querySelector("#tpl-title");
    const desc = document.querySelector("#tpl-desc");
    const downloadApk = document.querySelector("#tpl-download-apk");
    const downloadApkHover = document.querySelector("#tpl-download-apk-link");
    const downloadApkVersion = document.querySelector("#apk-version");
    const downloadApkTime = document.querySelector("#apk-update-time");
    const downloadApkMark = document.querySelector("#apk-update-mark");

    const adminLink = document.querySelector("#tpl-admin-link");

    const showpath = document.querySelector("#show-path");


    try {
      // 更新产品信息
      title.innerText = systemInfo.appName;
      desc.innerText = systemInfo.description || 'v' + systemInfo.appVersion;

      // 更新连接
      // adminLink.innerHTML =  config.ApplicationURL;
      adminLink.name = config.baseUrl;
      adminLink.href = config.baseUrl;


      // 更新二维码等
      // qrcodeImg.src = `${config.materialURL}/static/images/other/qrcode_h5_${config.CustomerID}.png`
      createAndMackQRcode(qrcodeImg, {
        width: 180,
        height: 180,
        colorDark: "black",
        colorLight: "#ffffff",
      },
        window.location.href
      );
    } catch (error) {
      console.warn('更新产品信息失败', error);
    }


    try {
      const pam = {
        appType: config.APP_TYPE_ID,
        appEdition: 1 // android/ios
      }
      const { data } = await getNewApp(pam);
      console.log('getNewApp:', data);
      if (/* false &&  */data && data.appUpdateUrl && data.appUpdateUrl.includes('.apk')) {
        downloadApk.href = data.appUpdateUrl;
        downloadApkHover.href = data.appUpdateUrl;
        // 生成下载二维码
        createAndMackQRcode(document.getElementById("apk-qrcode"), {
          width: 150,
          height: 150,
        }, data.appUpdateUrl);

        downloadApkVersion.innerText = `v${data.appVersionNum}`;
        downloadApkMark.innerText = `${data.updateContent}`;
        downloadApkTime.innerText = `${data.updateTime}`;

      } else {
        throw new Error('未在后台找到相关资源');
      }
    } catch (error) {
      downloadApk.onclick = () => { alert('未在后台找到相关资源'); return false; };
      downloadApk.href = "#";
      downloadApk.classList.add('disabled');

      downloadApkHover.onclick = () => { alert('未在后台找到相关资源'); return false; }
      const apkQrcode = document.getElementById("apk-qrcode");
      apkQrcode.style = "background-color: #ccc; color: #ccc;";
    }

    const timer = setInterval(() => {
      const currPath = document.getElementById("ifm").contentWindow.location.href;
      if (currPath != showpath.innerText)
        showpath.innerText = currPath;
    }, 1000);

    // 页面刷新时，重置定时器
    window.addEventListener('beforeunload', () => {
      clearInterval(timer);
    });

    // }
  }
})();


function createAndMackQRcode(el, options, url) {
  // 生成下载二维码
  // const QRCode = require('./qrcode.min.js');
  const qrcode = new QRCode(el, options);
  qrcode.makeCode(url);
}
